#include "pakge_response_json.h"
#include "storage_interface.h"
#include "cJSON.h"
#include <stdio.h>
#include <string.h>

#define JSON_CMD            "cmd"
#define JSON_USER           "user"
#define JSON_PWD            "pwd"
#define JSON_UART           "uart"
#define JSON_TYPE           "type"
#define JSON_MODE           "mode"
#define JSON_BAUD           "baud"
#define JSON_BIT            "bit"
#define JSON_STOP           "stop"
#define JSON_VERIFY         "verify"
#define JSON_SUB_STATION    "sub_station"
#define JSON_GET_CYCLE      "get_cycle"
// #define JSON_REPORT_CYCLE   "report_cycle"
#define JSON_DIDO           "dido"
#define JSON_DI_ALL         "di_all"
#define JSON_DO_ALL         "do_all"
#define JSON_DEVICE_NAME    "device_name"
#define JSON_NET_CONFIG     "net_config"
#define JSON_IP             "ip"
#define JSON_ADDR           "addr"
#define JSON_MASK           "mask"
#define JSON_GETEWAY        "geteway"
#define JSON_DNS_1          "dns_1"
#define JSON_DNS_2          "dns_2"
#define JSON_USER_ID        "user_id"
#define JSON_CERT           "cert"
#define JSON_NET_STATUS     "net_status"
#define JSON_DEVICE_ID      "device_id"
#define JSON_PRODUCTS_ID    "products_id"
#define JSON_SERVER         "server"
#define JSON_PORT           "port"
#define JSON_SECURELD       "secureld"
#define JSON_SECUREKEY      "secureKey"



#define RESPONSE_REPLY_MSG_MAX_LEN      (128)  /* 回复请求的消息长度 */
unsigned char def_data[RESPONSE_REPLY_MSG_MAX_LEN] = {0};

/* 响应成功JSON信息 */
unsigned char* default_reply_succedd_msg(unsigned int cmd)
{
    memset_s(def_data, RESPONSE_REPLY_MSG_MAX_LEN, 0, RESPONSE_REPLY_MSG_MAX_LEN);
    snprintf(def_data, RESPONSE_REPLY_MSG_MAX_LEN, "{\"cmd\":%d,\"msg\":0}", cmd);
    return def_data;
}
/* 响应失败JSON信息 */
unsigned char* default_reply_fail_msg(unsigned int cmd, unsigned int error)
{
    memset_s(def_data, RESPONSE_REPLY_MSG_MAX_LEN, 0, RESPONSE_REPLY_MSG_MAX_LEN);
    snprintf(def_data, RESPONSE_REPLY_MSG_MAX_LEN, "{\"cmd\":%d,\"msg\":%d}", cmd, error);
    return def_data;
}

/* 判断json中cmd是否正确，便于后续的继续解析 */
static error_e assert_cmd_correctly(cJSON *json_source, unsigned int cmd)
{
    cJSON *cmd_p = cJSON_GetObjectItem(json_source, JSON_CMD);
    if (NULL == cmd_p || !cJSON_IsNumber(cmd_p) || cmd_p->valueint != cmd){
        storage_debug("cjson parse err!\r\n");
        return STO_PARSE_FAIL;
    }
    return STO_SUCCESS;
}

/* 解析json中嵌套一级字符串 */
static error_e get_level_1_string(cJSON *json_source, unsigned char *data, unsigned int len,  unsigned char *name)
{
    /* 获取 用户名*/
    cJSON *json_sub = cJSON_GetObjectItem(json_source, name);
    if (NULL == json_sub || !cJSON_IsString(json_sub) || strlen(json_sub->valuestring) < 1 ){
        storage_debug("cjson parse err!\r\n");
        return STO_PARSE_FAIL;
    }
    unsigned int json_length = strlen(json_sub->valuestring);
    if (json_length + 1 > len) {
        storage_debug("Buffer too small for string copy!\r\n");
        return STO_BUFFER_TOO_SMALL; // 假设您有一个这样定义的错误代码
    }

    if(memcpy_s(data, len, json_sub->valuestring, json_length) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail!\r\n");
        return STO_MEM_CPY_FAIL;
    }

    data[json_length] = '\0';
    // cJSON_DeleteItemFromObject(json_source, name);      /* 删除子对象可以通过根对象进行删除，这里无需特别删除 */
    return STO_SUCCESS;
}

/* 解析json中嵌套一级数字 */
static error_e get_level_1_number(const cJSON *json_source, unsigned int *data,  unsigned char *name)
{
    /* 获取 用户名*/
    cJSON *json_sub = cJSON_GetObjectItem(json_source, name);
    if (NULL == json_sub || !cJSON_IsNumber(json_sub)){
        storage_debug("cjson parse err!, name:%s\r\n", name);
        return STO_PARSE_FAIL;
    }

    *data = json_sub->valueint;
    
    // cJSON_DeleteItemFromObject(json_source, name);      /* 删除子对象可以通过根对象进行删除，这里无需特别删除 */
    return STO_SUCCESS;
}

/* 解析json中嵌套一级数字 */
static error_e get_level_1_double(cJSON *json_source, double *data,  unsigned char *name)
{
    /* 获取 用户名*/
    cJSON *json_sub = cJSON_GetObjectItem(json_source, name);
    if (NULL == json_sub || !cJSON_IsNumber(json_sub)){
        storage_debug("cjson parse err!, name:%s\r\n", name);
        return STO_PARSE_FAIL;
    }
    // double temp = (double)cJSON_GetNumberValue(json_sub);
    *data = (double)cJSON_GetNumberValue(json_sub);
    // cJSON_DeleteItemFromObject(json_source, name);      /* 删除子对象可以通过根对象进行删除，这里无需特别删除 */
    return STO_SUCCESS;
}


/* ********************************* 以下为具体接口实现 ********************************* */

/* 解析登录信息，并比较是否正确 */
error_e parse_login_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_LOGIN_CHECK) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    login_data_t login_ctx = {0};
    /* 解析 用户名 */
    if (get_level_1_string(root_p, login_ctx.user, sizeof(login_ctx.user), JSON_USER) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    /* 解析 密码 */
    if (get_level_1_string(root_p, login_ctx.pwd, sizeof(login_ctx.pwd), JSON_PWD) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    
    /* 读取 文件系统中存储的数据 */
    login_data_t storage_ctx = {0};
    get_storage_cfg_info(LOGIN_FILE_NEME, &storage_ctx, sizeof(login_data_t), DIDO_DEFAULT);

    /* 比较 登录信息 */
    if(strcmp(login_ctx.user, storage_ctx.user) == 0 && strcmp(login_ctx.pwd, storage_ctx.pwd) == 0)
    {
        cJSON_Delete(root_p);
        return STO_SUCCESS;
    }

    cJSON_Delete(root_p);    
    return STO_ACCOUNT_ERROR;
}

/* 设置登录信息 */
error_e parse_set_login_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_SET_LOGIN_CONFIG) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    login_data_t login_ctx = {0};
    /* 解析 用户名 */
    if (get_level_1_string(root_p, login_ctx.user, sizeof(login_ctx.user), JSON_USER) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    /* 解析 密码 */
    if (get_level_1_string(root_p, login_ctx.pwd, sizeof(login_ctx.pwd), JSON_PWD) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    
    /* 存储登录数据 */
    set_storage_cfg_info(LOGIN_FILE_NEME, &login_ctx, sizeof(login_data_t));

    cJSON_Delete(root_p);    
    return STO_SUCCESS;    
}

/* 打包uart和dido状态信息 */
error_e pakge_uart_config_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    uart_config_t uart_ctx = {0};
    ret = get_storage_cfg_info(UART_FILE_NEME, &uart_ctx, sizeof(uart_config_t), DIDO_DEFAULT);

    dido_config_t dido_ctx = {0};
    ret += get_storage_cfg_info(DIDO_FILE_NEME, &dido_ctx, sizeof(dido_config_t), DIDO_REALTIME);
    if (ret != STO_SUCCESS)
    {
        storage_debug("read uart and dido config is fail\r\n");
        return STO_NO_DATA;
    }
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }
    
    cJSON *uartArray = cJSON_CreateArray();   /* uart节点 */
    cJSON *uartObject = cJSON_CreateObject();
    if (uartArray == NULL || uartObject == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        cJSON_Delete(root_p);
        return STO_NO_MEMONY;
    }

    cJSON *didoArray = cJSON_CreateArray();   /* dido节点 */
    cJSON *didoObject = cJSON_CreateObject();
    if (didoArray == NULL || didoObject == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        cJSON_Delete(root_p);
        cJSON_Delete(uartArray);
        cJSON_Delete(uartObject);
        return STO_NO_MEMONY;
    }

    cJSON_AddItemToObject(uartObject, JSON_TYPE, cJSON_CreateNumber(uart_ctx.type));
    cJSON_AddItemToObject(uartObject, JSON_MODE, cJSON_CreateNumber(uart_ctx.mode));
    cJSON_AddItemToObject(uartObject, JSON_BAUD, cJSON_CreateNumber(uart_ctx.baud));
    cJSON_AddItemToObject(uartObject, JSON_BIT, cJSON_CreateNumber(uart_ctx.data_bit));
    cJSON_AddItemToObject(uartObject, JSON_STOP, cJSON_CreateNumber(uart_ctx.stop_bit));
    cJSON_AddItemToObject(uartObject, JSON_VERIFY, cJSON_CreateNumber(uart_ctx.verify));
    cJSON_AddItemToObject(uartObject, JSON_SUB_STATION, cJSON_CreateNumber(uart_ctx.sub_station));
    cJSON_AddItemToObject(uartObject, JSON_GET_CYCLE, cJSON_CreateNumber(uart_ctx.get_cycle));
    // cJSON_AddItemToObject(uartObject, JSON_REPORT_CYCLE, cJSON_CreateNumber(uart_ctx.report_cycle));
    cJSON_AddItemToArray(uartArray, uartObject); // 将 uartObject 添加到数组中

    cJSON_AddItemToObject(didoObject, JSON_DI_ALL, cJSON_CreateNumber(dido_ctx.di_val.val));
    cJSON_AddItemToObject(didoObject, JSON_DO_ALL, cJSON_CreateNumber(dido_ctx.do_val.val));
    // cJSON_AddItemToObject(didoObject, JSON_GET_CYCLE, cJSON_CreateNumber(dido_ctx.get_cycle));
    // cJSON_AddItemToObject(didoObject, JSON_REPORT_CYCLE, cJSON_CreateNumber(dido_ctx.report_cycle));
    cJSON_AddItemToArray(didoArray, didoObject); // 将 didoObject 添加到数组中

    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_UART_CONFIG));
    cJSON_AddItemToObject(root_p, JSON_UART, uartArray);
    cJSON_AddItemToObject(root_p, JSON_DIDO, didoArray);

    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 解析uart和dido配置信息 */
error_e parse_uart_config_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);

    if (NULL == root_p) {
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_POST_UART_CONFIG) != STO_SUCCESS) {
        cJSON_Delete(root_p); // 确保释放内存
        return STO_PARSE_FAIL;
    }

    cJSON *temp_uart_p = cJSON_GetObjectItem(root_p, JSON_UART);
    cJSON *temp_dido_p = cJSON_GetObjectItem(root_p, JSON_DIDO);
    if (NULL == temp_uart_p || NULL == temp_dido_p) {
        storage_debug("JSON does not contain required items!\n");
        cJSON_Delete(root_p); // 确保释放内存
        return STO_PARSE_FAIL;
    }

    cJSON *uart_p = cJSON_GetArrayItem(temp_uart_p, 0);
    cJSON *dido_p = cJSON_GetArrayItem(temp_dido_p, 0);
    if (NULL == uart_p || NULL == dido_p) {
        storage_debug("cjson parse err!\n");
        cJSON_Delete(root_p); // 确保释放内存
        return STO_PARSE_FAIL;
    }

    /* 解析串口配置 */
    uart_config_t uart_ctx = {0};
    if (get_level_1_number(uart_p, &uart_ctx.type, JSON_TYPE) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.mode, JSON_MODE) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.baud, JSON_BAUD) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.data_bit, JSON_BIT) != STO_SUCCESS ||
        get_level_1_double(uart_p, &uart_ctx.stop_bit, JSON_STOP) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.verify, JSON_VERIFY) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.sub_station, JSON_SUB_STATION) != STO_SUCCESS ||
        get_level_1_number(uart_p, &uart_ctx.get_cycle, JSON_GET_CYCLE) != STO_SUCCESS) {
        cJSON_Delete(root_p); // 确保释放内存
        return STO_STORAGE_FAILED;
    }

    /* 解析dido配置 */
    dido_config_t dido_ctx = {0};
    if (get_level_1_number(dido_p, &dido_ctx.do_val.val, JSON_DO_ALL) != STO_SUCCESS) {
        cJSON_Delete(root_p); // 确保释放内存
        return STO_STORAGE_FAILED;
    }

    /* 存储串口配置信息 */
    if(set_storage_cfg_info(UART_FILE_NEME, &uart_ctx, sizeof(uart_config_t)) != STO_SUCCESS) {
        cJSON_Delete(root_p); // 确保释放内存
        return STO_STORAGE_FAILED;
    }

    /* 存储dido配置信息 */
    if(set_storage_cfg_info(DIDO_FILE_NEME, &dido_ctx, sizeof(dido_config_t)) != STO_SUCCESS) {
        cJSON_Delete(root_p); // 确保释放内存
        return STO_STORAGE_FAILED;
    }

    cJSON_Delete(root_p); // 确保释放内存
    return STO_SUCCESS;
}

/* 打包network info信息 */
error_e pakge_net_info_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    network_config_t net_ctx = {0};
    ret = get_storage_cfg_info(NET_FILE_NEME, &net_ctx, sizeof(network_config_t), DIDO_DEFAULT);    
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }

    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_NETWORK_INFO));
    cJSON_AddItemToObject(root_p, JSON_DEVICE_NAME, cJSON_CreateString(net_ctx.name));
    cJSON_AddItemToObject(root_p, JSON_NET_CONFIG, cJSON_CreateNumber(net_ctx.status));

    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 打包network配置信息 */
error_e pakge_net_config_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    network_config_t net_ctx = {0};
    ret = get_storage_cfg_info(NET_FILE_NEME, &net_ctx, sizeof(network_config_t), DIDO_DEFAULT);    
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }
    
    cJSON *ip_p =  cJSON_CreateObject();   /* uart节点 */
    if (ip_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        cJSON_Delete(root_p);
        return STO_NO_MEMONY;
    }

    /* 处理IP地址 */
    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    int_to_ip(net_ctx.ip_addr, ip);
    cJSON_AddItemToObject(ip_p, JSON_ADDR, cJSON_CreateString(ip));

    /* 处理掩码 */
    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    int_to_ip(net_ctx.ip_mask, ip);
    cJSON_AddItemToObject(ip_p, JSON_MASK, cJSON_CreateString(ip));

    /* 处理网关 */
    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    int_to_ip(net_ctx.ip_gateway, ip);
    cJSON_AddItemToObject(ip_p, JSON_GETEWAY, cJSON_CreateString(ip));

    /* 处理DNS1 */
    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    int_to_ip(net_ctx.ip_dns1, ip);
    cJSON_AddItemToObject(ip_p, JSON_DNS_1, cJSON_CreateString(ip));

    /* 处理DNS2 */
    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    int_to_ip(net_ctx.ip_dns2, ip);
    cJSON_AddItemToObject(ip_p, JSON_DNS_2, cJSON_CreateString(ip));    
    
    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_NETWORK_CONFIG));
    cJSON_AddItemToObject(root_p, JSON_DEVICE_NAME, cJSON_CreateString(net_ctx.name));
    cJSON_AddItemToObject(root_p, JSON_NET_CONFIG, cJSON_CreateNumber(net_ctx.status));
    cJSON_AddItemToObject(root_p, JSON_IP, ip_p);

    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 解析net配置信息 */
error_e parse_net_config_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_POST_NETWORK_CONFIG) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    /* 解析网络配置 */
    network_config_t ip_ctx = {0};
    if (get_level_1_string(root_p, ip_ctx.name, sizeof(ip_ctx.name), JSON_DEVICE_NAME) != STO_SUCCESS)
    {
        return STO_PARSE_FAIL;
    }

    cJSON *ip_p = cJSON_GetObjectItem(root_p, JSON_IP);
    if (NULL == ip_p){
        storage_debug("cjson parse err!\n");
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    if (get_level_1_string(ip_p, ip, IP_ADDR_STR_LEN, JSON_ADDR) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ip_ctx.ip_addr = ip_to_int(ip);

    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    if (get_level_1_string(ip_p, ip, IP_ADDR_STR_LEN, JSON_MASK) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ip_ctx.ip_mask = ip_to_int(ip);

    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    if (get_level_1_string(ip_p, ip, IP_ADDR_STR_LEN, JSON_GETEWAY) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ip_ctx.ip_gateway = ip_to_int(ip);

    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    if (get_level_1_string(ip_p, ip, IP_ADDR_STR_LEN, JSON_DNS_1) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ip_ctx.ip_dns1 = ip_to_int(ip);

    memset_s(ip, IP_ADDR_STR_LEN, 0, IP_ADDR_STR_LEN);
    if (get_level_1_string(ip_p, ip, IP_ADDR_STR_LEN, JSON_DNS_2) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ip_ctx.ip_dns2 = ip_to_int(ip);

    /* 判定网络是否配置 */
    if (ip_ctx.ip_addr != 0 && ip_ctx.ip_mask != 0 && ip_ctx.ip_gateway != 0)
    {
        ip_ctx.status = CONFIGURED;
    }

    /* 存储网络配置信息 */
    if(set_storage_cfg_info(NET_FILE_NEME, &ip_ctx, sizeof(network_config_t)) != STO_SUCCESS)
    {
        cJSON_Delete(root_p); 
        return STO_STORAGE_FAILED;
    }

    cJSON_Delete(root_p);    
    return STO_SUCCESS;
}

/* 打包voucher配置信息 */
error_e pakge_voucher_config_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    voucher_config_t cert_ctx = {0};
    ret = get_storage_cfg_info(VOUCHER_FILE_NEME, &cert_ctx, sizeof(voucher_config_t), DIDO_DEFAULT);    
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }
    
    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_VOUCHER_CONFIG));
    cJSON_AddItemToObject(root_p, JSON_USER_ID, cJSON_CreateString(cert_ctx.user_id));
    cJSON_AddItemToObject(root_p, JSON_CERT, cJSON_CreateString(cert_ctx.cert));
    cJSON_AddItemToObject(root_p, JSON_NET_STATUS, cJSON_CreateNumber(get_softbus_network_status()));


    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 解析voucher配置信息 */
error_e parse_voucher_config_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_POST_VOUCHER_CONFIG) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    voucher_config_t cert_ctx = {0};
    /* 解析 user id */
    if (get_level_1_string(root_p, cert_ctx.user_id, sizeof(cert_ctx.user_id), JSON_USER_ID) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    /* 解析 凭证 */
    if (get_level_1_string(root_p, cert_ctx.cert, sizeof(cert_ctx.cert), JSON_CERT) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    
    /* 存储凭证数据 */
    set_storage_cfg_info(VOUCHER_FILE_NEME, &cert_ctx, sizeof(voucher_config_t));

    cJSON_Delete(root_p);    
    return STO_SUCCESS;
}

/* 打包ntp配置信息 */
error_e pakge_ntp_config_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    ntp_config_t ntp_ctx = {0};
    ret = get_storage_cfg_info(NTP_FILE_NEME, &ntp_ctx, sizeof(ntp_config_t), DIDO_DEFAULT);    
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }
    
    /* 处理IP地址 */
    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    int_to_ip(ntp_ctx.server_addr, ip);

    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_NTP_SERVER_CONFIG));
    cJSON_AddItemToObject(root_p, JSON_SERVER, cJSON_CreateString(ip));
    // cJSON_AddItemToObject(root_p, JSON_PORT, cJSON_CreateNumber(ntp_ctx.server_port));

    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 解析ntp配置信息 */
error_e parse_ntp_config_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_POST_NTP_SERVER_CONFIG) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    ntp_config_t ntp_ctx = {0};
    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    if (get_level_1_string(root_p, ip, IP_ADDR_STR_LEN, JSON_SERVER) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ntp_ctx.server_addr = ip_to_int(ip);
    
    /* 存储NTP配置 */
    set_storage_cfg_info(NTP_FILE_NEME, &ntp_ctx, sizeof(ntp_config_t));

    cJSON_Delete(root_p);    
    return STO_SUCCESS;
}

/* 打包ota配置信息 */
error_e pakge_ota_config_json(unsigned char *json, unsigned int len)
{
    error_e ret;
    ota_config_t ota_ctx = {0};
    ret = get_storage_cfg_info(OTA_FILE_NEME, &ota_ctx, sizeof(ota_config_t), DIDO_DEFAULT);    
    
    cJSON *root_p =  cJSON_CreateObject();   /* 根节点 */
    if (root_p == NULL)
    {
        storage_debug("no memony create json obj\r\n");
        return STO_NO_MEMONY;
    }
    
    /* 处理IP地址 */
    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    int_to_ip(ota_ctx.server_addr, ip);

    cJSON_AddItemToObject(root_p, JSON_CMD, cJSON_CreateNumber(CMD_GET_OTA_SERVER_CONFIG));
    cJSON_AddItemToObject(root_p, JSON_DEVICE_ID, cJSON_CreateString(ota_ctx.device_id));
    cJSON_AddItemToObject(root_p, JSON_PRODUCTS_ID, cJSON_CreateString(ota_ctx.products_id));
    cJSON_AddItemToObject(root_p, JSON_SERVER, cJSON_CreateString(ip));
    cJSON_AddItemToObject(root_p, JSON_PORT, cJSON_CreateNumber(ota_ctx.server_port));
    cJSON_AddItemToObject(root_p, JSON_SECURELD, cJSON_CreateString(ota_ctx.secureld));
    cJSON_AddItemToObject(root_p, JSON_SECUREKEY, cJSON_CreateString(ota_ctx.secureKey));

    if(memcpy_s(json, len, cJSON_Print(root_p), strlen(cJSON_Print(root_p))) != STO_SUCCESS)
    {
        storage_debug("memcpy_s is fail\r\n");
        cJSON_Delete(root_p);
        return STO_MEM_CPY_FAIL;
    }

    cJSON_Delete(root_p);
    return STO_SUCCESS;
}

/* 解析ota配置信息 */
error_e parse_ota_config_json(const unsigned char* json)
{
    cJSON *root_p = cJSON_Parse(json);
    if (NULL == root_p){
        storage_debug("cjson parse err!\n");
        return STO_PARSE_FAIL;
    }

    /* 判断CMD */
    if (assert_cmd_correctly(root_p, CMD_POST_OTA_SERVER_CONFIG) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    ota_config_t ota_ctx = {0};
    if (get_level_1_string(root_p, ota_ctx.device_id, sizeof(ota_ctx.device_id), JSON_DEVICE_ID) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    if (get_level_1_string(root_p, ota_ctx.products_id, sizeof(ota_ctx.products_id), JSON_PRODUCTS_ID) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    unsigned char ip[IP_ADDR_STR_LEN] = {0};
    if (get_level_1_string(root_p, ip, IP_ADDR_STR_LEN, JSON_SERVER) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }
    ota_ctx.server_addr = ip_to_int(ip);

    if (get_level_1_number(root_p, &ota_ctx.server_port, JSON_PORT) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    if (get_level_1_string(root_p, ota_ctx.secureld, sizeof(ota_ctx.secureld), JSON_SECURELD) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    if (get_level_1_string(root_p, ota_ctx.secureKey, sizeof(ota_ctx.secureKey), JSON_SECUREKEY) != STO_SUCCESS)
    {
        cJSON_Delete(root_p);
        return STO_PARSE_FAIL;
    }

    /* 存储NTP配置 */
    set_storage_cfg_info(OTA_FILE_NEME, &ota_ctx, sizeof(ota_config_t));

    cJSON_Delete(root_p);    
    return STO_SUCCESS;
}
